//
//  YBCloudComputerCell.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/8.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

class YBCloudComputerCell: UITableViewCell {
    
    /// Power on Block
    var powerOnBlock: (() -> Void)?
    var faultRepairBtnBlock: (() -> Void)?
    var resetBtnBlock: (() -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.backgroundColor = .white
        contentView.backgroundColor = .white
        
        desLabel.numberOfLines = 0
        
        contentView.addSubviews([titleLabel, desLabel, imgView, powerOnBtn])

        titleLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalTo(16)
            make.height.equalTo(20)
        }
        
        desLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.left.right.equalToSuperview().inset(15)
        }
        
        imgView.snp.makeConstraints { make in
            make.size.equalTo(66)
            make.right.equalTo(-5)
            make.centerY.equalToSuperview()
        }
        
        powerOnBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(89, 30))
            make.left.equalTo(15)
            make.top.equalTo(desLabel.snp.bottom).offset(11)
            make.bottom.equalTo(-15).priority(.medium)
        }
    }
    
    /// 重写frame
    override var frame: CGRect {
        get { return super.frame }
        set {
            var frame = newValue
            frame.origin.x += 10
            frame.size.width -= 2*10
            super.frame = frame
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var imgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_cloudcomputer_p1"))
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var titleLabel = self.bk_addLabel(font: .systemFont(ofSize: 15, weight: .semibold), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    private lazy var desLabel = self.bk_addLabel(font: .systemFont(ofSize: 12, weight: .regular), bgColor: .clear, textColor: ColorDef.gray102)
    
    private lazy var powerOnBtn: BKLayoutButton = {
        let btn = self.bk_addGradientLayoutButton(style: .leftTitleRightImage, space: 6, bgColor: .clear, imageSize: CGSizeMake(16, 8), image: UIImage(named: "icon_cloudcomputer_rightarrow"), titleFont: .systemFont(ofSize: 14, weight: .semibold), title: "开机", titleColor: .white, gradientSize: CGSizeMake(89, 30), colors: [kRGBColor(88, 182, 255), kRGBColor(40, 128, 237)])
        btn.isUserInteractionEnabled = true
        btn.bk_addTarget { [weak self] button in
            self?.powerOnBlock?()
        }
        return btn
    }()
}

// MARK: - Public
extension YBCloudComputerCell {
    
    func updateCell(by model: YBUserModel.OrderInfoVos) {
        titleLabel.text = model.productName
        desLabel.text = model.cpu + "CPU" + "\t" + model.memory + "内存" + "\t" + model.hardPan + "硬盘"
        
        if model.purchase {
            powerOnBtn.setTitle("开机", for: .normal)
        }else {
            powerOnBtn.setTitle("购买", for: .normal)
        }
    }
    
}
